'''
Created on 2009-11-23

@author: Jialai_Zhu
'''
import urllib2
import urllib           
class urllib2Downloader(object):
#    version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'
    def __init__(self, proxy=None):
        self.proxy = proxy
#        self.type=type
#        self.url=url
#        self.internet=internet
#        if parent!=None:
#            self.ref=parent.url

    def run(self, url, parent=None, internet=True):
        self.page = ''        
        if internet:
            if self.proxy != None:                
                proxy_handler = urllib2.ProxyHandler({'http': self.proxy})
                opener = urllib2.build_opener(proxy_handler)
            else:
                opener = urllib2.build_opener()
            
            req = urllib2.Request(urllib.unquote(url))
            if parent != None:
                req.add_header('Referer', parent.url)
    
            urllib2.install_opener(opener)    
            page = urllib2.urlopen(req)
#            except HTTPError, e:  
#      print 'Error code: ', e.code  
            while 1:
                s = page.read()
                if not s:
                    page.close()
                    return self.page
                self.page += s
#            data=''
#            ffp=self.open(url,data)
#            self.close()
        else:
            return self.page
